window.addEventListener("load", () => {
    $.post("/user/catUserAccount",
        null,
        function (res) {
            if (res.status == "3001") {
                alert(res.message)
                document.location = "/public/html/home.html"
            }
        }, "json");

    if (Cookies.get('currentTaskID')) {
        let h2 = document.createElement("h2")
        h2.id = "currentTaskNotice"
        let span1 = document.createElement("span")
        span1.classList.add("label")
        span1.classList.add("label-default")
        span1.style.backgroundColor = "#333333"
        span1.innerText = `Task Name:${Cookies.get('currentTaskName')} Task ID:${Cookies.get('currentTaskID')}`
        let span2 = document.createElement("span")
        span2.classList.add("label")
        span2.classList.add("label-default")
        let due_sessonds = Cookies.get('currentTaskDueDate')

        span2.innerText = `Catagory:${Cookies.get('currentTaskcatagory')} Marks:${Cookies.get('currentTaskMarks')}`
        let span3 = document.createElement("span")
        span3.id = "timeGenerate"
        span3.classList.add("label")
        span3.classList.add("label-default")
        h2.appendChild(span1)
        h2.appendChild(span2)
        h2.appendChild(span3)
        let interval = setInterval((resolve) => {
            rest_sessonds = due_sessonds - Math.round(new Date().getTime() / 1000)
            if (rest_sessonds <= 0) {
                clearInterval(interval);
                alert("This Task has expired,Please Finish it AS SOON!")

            } else {
                document.getElementById("timeGenerate").innerText = `The Rest Seconds: ${rest_sessonds}s`
            }


        }, 1000)


        document.getElementById("dashboard_row2").appendChild(h2)

        let descriptionDIV = document.createElement("div")
        descriptionDIV.id="descriptionDIV"
        let descriptionTitle = document.createElement("h3")
        descriptionTitle.innerText="DESCRIPTION"
        descriptionTitle.style.borderBottom="1px solid black"
        descriptionDIV.appendChild(descriptionTitle)
        let descriptionText = document.createElement("input")
        descriptionText.setAttribute("type","text")
        descriptionText.setAttribute("readonly","readonly")
        descriptionText.classList.add("form-control");
        descriptionText.value=Cookies.get("currentTaskDescription")
        descriptionDIV.appendChild(descriptionText)
        let file = document.createElement("input")
        file.id="fileElement"
        file.setAttribute("type","file")
        file.setAttribute("name","upLoadFile")
        file.innerText="ADD FILE"
        /*
        * <form id="uploadForm">
    <input id="fileContent" type="file" name="upLoadFile"/>
    <button id="fileButton" type="button">Submit</button>
</form>
*/
        let fileUploadDIV = document.createElement("form")
        fileUploadDIV.id="fileUpload"
        let fileUploadTitle = document.createElement("h3")
        fileUploadTitle.innerText="FILE UPLOAD"
        fileUploadTitle.style.borderBottom="1px solid black"
        let fileUploadButton = document.createElement("button")
        fileUploadButton.setAttribute("type","button")
        fileUploadButton.innerText="Submit File"
        fileUploadButton.addEventListener("click",()=>{
         let fileElement = document.getElementById("fileElement");
         let fileObject = fileElement.files[0]
         let formData = new FormData();
         formData.append("file", fileObject)

         $.ajax({
             url: '/user/upload',
             type: 'POST',
             cache: false,    //上传文件不需要缓存
             data: formData,
             processData: false, // 告诉jQuery不要去处理发送的数据
             contentType: false, // 告诉jQuery不要去设置Content-Type请求头
             success: function (res) {
                 if(!res.status.startsWith("4")){
                     alert("Upload Sucess")
                     document.getElementById("submit_description").value=document.getElementById("submit_description").value+`[${fileObject.name}]`;
                 }else {
                     alert("Upload Failure")
                 }
             }
         })
     })
        fileUploadDIV.setAttribute("action","/user/fileUpload")
        fileUploadDIV.setAttribute("method","post")
        fileUploadDIV.setAttribute("enctype","multipart/form-data")
        fileUploadDIV.appendChild(fileUploadTitle)
        fileUploadDIV.appendChild(file)
        fileUploadDIV.appendChild(fileUploadButton)
        document.getElementById("dashboard_row3").appendChild(descriptionDIV)
        document.getElementById("dashboard_row3").appendChild(fileUploadDIV)
        let submitDIV =  document.createElement("div")
       submitDIV.id="submit_div"

        let submit_description_title = document.createElement("h3")
        submit_description_title.id="submit_title"

        submit_description_title.innerText="SUBMIT DESCRIPTION"
        submit_description_title.style.borderBottom="1px solid black"
        let submit_description_textarea = document.createElement("textarea")
        submit_description_textarea.id="submit_description"
        submit_description_textarea.classList.add("form-control")
        submit_description_textarea.setAttribute("row","5")
        submitDIV.appendChild(submit_description_title)
        submitDIV.appendChild(submit_description_textarea)

        let submitTaskButton = document.createElement("input")
        submitTaskButton.classList.add("btn")
        submitTaskButton.classList.add("btn-default")
        submitTaskButton.setAttribute("type","button")
        submitTaskButton.setAttribute("value","Submit Task")
        submitTaskButton.addEventListener("click",(event)=>{

            let taskid = Cookies.get('currentTaskID')
            let  submit_description = document.getElementById("submit_description").value;
            $.post("/user/submitTask",{
                "submit_description":submit_description,
                "taskid":taskid
            },function (res) {
                if (res) {
                    Cookies.remove("currentTaskID")
                    alert("Submit Task Sucess")
                    document.location.reload();
                }
            },"json")
        })
        document.getElementById("dashboard_row3").appendChild(submitTaskButton)
        submitDIV.appendChild(submitTaskButton)
        document.getElementById("dashboard_row3").appendChild(submitDIV)


    } else {
        let h2 = document.createElement("h2")
        h2.id = "currentTaskNotice"
        h2.innerText = "No tasks In Progress,Please Select One From DashBoard"
        document.getElementById("dashboard_row2").appendChild(h2)
    }
})